package com.rq.adapter.ui;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.rq.adapter.ExampleRedViewHolder;
import com.rq.adapter.LOG;
import com.rq.adapter.MoreData;
import com.rq.adapter.R;
import com.rq.rvlibrary.BaseAdapter;
import com.rq.rvlibrary.RecyclerUtil;

import java.util.ArrayList;
import java.util.List;

public class DiffUtilActivity extends Activity {

    BaseAdapter diffUtilAdapter;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_diff);
        recyclerView = findViewById(R.id.rv);
    }

    public void testDiffCallback(View view) {
        makeAdapter();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                LOG.e("MainActivity", "251:");
                final List<MoreData> newData = BaseAdapter.deepCopy(diffUtilAdapter.getData());
                MoreData old = newData.remove(3);
                old.name = old.name + "#";
                newData.add(3, old);
                diffUtilAdapter.setData(newData);
            }
        }).start();
    }

    private void makeAdapter() {
        if (diffUtilAdapter == null) {
            final List<String> old = getDebugData();
            diffUtilAdapter = new BaseAdapter(this, R.layout.item_red, ExampleRedViewHolder.class);
            diffUtilAdapter.openDiffUtil();
            diffUtilAdapter.setData(old);
        }
        if (recyclerView.getAdapter() != diffUtilAdapter) {
            new RecyclerUtil(diffUtilAdapter).set2View(recyclerView);
        }
    }

    public void testRemove3(View view) {
        makeAdapter();
        diffUtilAdapter.removeItem(3);
    }

    public void testAdd(View view) {
        makeAdapter();
        diffUtilAdapter.addData(3, new MoreData("1  $$$$", 10 + diffUtilAdapter.getDataSize()));
    }

    private List getDebugData() {
        List debug = new ArrayList<>();
        debug.add(new MoreData("01     ##", 12));
        debug.add(new MoreData("02     ##", 12));
        debug.add(new MoreData("03     ##", 12));
        debug.add(new MoreData("04     ##", 12));
        debug.add(new MoreData("05     ##", 12));
        debug.add(new MoreData("06     ##", 12));
        debug.add(new MoreData("07     ##", 12));
        debug.add(new MoreData("08     ##", 12));
        debug.add(new MoreData("09     ##", 12));
        debug.add(new MoreData("10     ##", 12));
        debug.add(new MoreData("11     ##", 12));
        debug.add(new MoreData("12     ##", 12));
        debug.add(new MoreData("13     ##", 12));
        debug.add(new MoreData("14     ##", 12));
        debug.add(new MoreData("15     ##", 12));
        debug.add(new MoreData("16     ##", 12));
        return debug;
    }
}
